package com.rj1101.hscrm.demo.dao;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.rj1101.hscrm.demo.model.Appointmentinfo;
import com.rj1101.hscrm.demo.model.Treatment;
import com.rj1101.hscrm.demo.util.Paging;

/**
 * A data access object (DAO) providing persistence and search support for
 * Appointmentinfo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.rj1101.hscrm.demo.model.Appointmentinfo
 * @author MyEclipse Persistence Tools
 */

public class AppointmentinfoDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(AppointmentinfoDAO.class);
	// property constants
	public static final String APPOINT_DATE = "appointDate";
	public static final String VALID_DATE = "validDate";
	public static final String TID = "tid";
	public static final String APPOINT_CUSTOMER = "appointCustomer";
	public static final String APPOINT_TYPE = "appointType";
	public static final String APPOINT_DOCTOR = "appointDoctor";
	public static final String TREAT_DATE = "treatDate";

	protected void initDao() {
		// do nothing
	}

	public void save(Appointmentinfo transientInstance) {
		log.debug("saving Appointmentinfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Appointmentinfo persistentInstance) {
		log.debug("deleting Appointmentinfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Appointmentinfo findById(java.lang.Integer id) {
		log.debug("getting Appointmentinfo instance with id: " + id);
		try {
			Appointmentinfo instance = (Appointmentinfo) getHibernateTemplate()
					.get("com.rj1101.hscrm.demo.model.Appointmentinfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Appointmentinfo instance) {
		log.debug("finding Appointmentinfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Appointmentinfo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Appointmentinfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByAppointDate(Object appointDate) {
		return findByProperty(APPOINT_DATE, appointDate);
	}
	
	public List findByAppointDateTime(String start ,String end){
		log.debug("findByAppointDateTime");
		String hql="from  Appointmentinfo as app where app.appointDate>="+start+" and app.appointDate<= "+end;
		try {
			List results = getHibernateTemplate().find(hql);
			return results;
		} catch (RuntimeException e) {
			// TODO: handle exception
			log.error("find by property name failed", e);
			throw e;
		}
	}
	public List findByValidDate(Object validDate) {
		return findByProperty(VALID_DATE, validDate);
	}

	public List findByTid(Object tid) {
		return findByProperty(TID, tid);
	}

	public List findByAppointCustomer(Object appointCustomer) {
		return findByProperty(APPOINT_CUSTOMER, appointCustomer);
	}

	public List findByAppointType(Object appointType) {
		return findByProperty(APPOINT_TYPE, appointType);
	}

	public List findByAppointDoctor(Object appointDoctor) {
		return findByProperty(APPOINT_DOCTOR, appointDoctor);
	}

	public List findByTreatDate(Object treatDate) {
		return findByProperty(TREAT_DATE, treatDate);
	}

	public List findAll() {
		log.debug("finding all Appointmentinfo instances");
		try {
			String queryString = "from Appointmentinfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Appointmentinfo merge(Appointmentinfo detachedInstance) {
		log.debug("merging Appointmentinfo instance");
		try {
			Appointmentinfo result = (Appointmentinfo) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Appointmentinfo instance) {
		log.debug("attaching dirty Appointmentinfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	
	public void attachClean(Appointmentinfo instance) {
		log.debug("attaching clean Appointmentinfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static AppointmentinfoDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (AppointmentinfoDAO) ctx.getBean("AppointmentinfoDAO");
	}
	
	public List<Appointmentinfo> pagingAppointInfo(String tableName, Paging pager) {
		log.debug("find treatment info by paging...");
		try{
			Session session = getHibernateTemplate().getSessionFactory().openSession();
			String sql="SELECT TOP "+pager.getNumPerPage()+" * FROM "+tableName+
					" WHERE "+pager.getOrderField()+" >= " +
					"(SELECT TOP 1 "+pager.getOrderField()+
					" FROM (SELECT TOP "+(pager.getNumPerPage()*(pager.getPageNum()-1)+1)+" "+
					pager.getOrderField()+" FROM "+tableName+" ORDER BY "+pager.getOrderField()+
					" ASC) AS TEMP " +
					"ORDER BY "+pager.getOrderField()+" DESC)";
			Query query = session.createSQLQuery(sql).addEntity(Appointmentinfo.class);
			return query.list();
		}catch(RuntimeException re){
			log.error("find treatment info by paging failed.", re);
			throw re;
		}
	}
}